/////////////////////////////////////////////
// CMAWinReact custom object.

#include "StdAfx.h"
#include "StdArx.h"
#include "dbproxy.h"
#include "geassign.h"
#include "acgi.h"

#define VERSION_CMAWINREACT 1


//{{AFX_ARX_MACRO
ACRX_DXF_DEFINE_MEMBERS(CMAWinReact, AcDbObject,
                        AcDb::kDHL_CURRENT, AcDb::kMReleaseCurrent,
                        AcDbProxyObject::kNoOperation,
                        CMAWINREACT, CMACh9_3
);
//}}AFX_ARX_MACRO


// Constructor / Destructor

CMAWinReact::CMAWinReact()
{

}

CMAWinReact::CMAWinReact(AcDbObjectId objId)
{
	m_ObjId = objId;
}


CMAWinReact::~CMAWinReact()
{

}

void CMAWinReact::setLwrLeftPoint(AcGePoint3d llPt)
{
	m_llPt = llPt;
}


Acad::ErrorStatus CMAWinReact::dwgInFields(AcDbDwgFiler* pFiler)
{
	assertWriteEnabled();
	Acad::ErrorStatus es;

	// Call dwgInFields from AcDbObject
	if ((es = AcDbObject::dwgInFields(pFiler)) != Acad::eOk) {
		return es;
	}

	// Read version number.
	Adesk::UInt16 version;
	pFiler->readItem(&version);
	if (version > VERSION_CMAWINREACT)
		return Acad::eMakeMeProxy;

	// Read the data members.
	switch (version)
	{
		case (1):

			pFiler->readItem(&m_llPt);
			pFiler->readItem((AcDbSoftPointerId*)&m_ObjId);

		break;
	}


	return pFiler->filerStatus();
}

Acad::ErrorStatus CMAWinReact::dwgOutFields(AcDbDwgFiler* pFiler) const
{
	assertReadEnabled();
	Acad::ErrorStatus es;

	// Call dwgOutFields from AcDbObject
	if ((es = AcDbObject::dwgOutFields(pFiler)) != Acad::eOk) {
		return es;
	}

	// Write version number.
	pFiler->writeItem((Adesk::UInt16) VERSION_CMAWINREACT);

	// Write the data members.
	pFiler->writeItem(m_llPt);
	pFiler->writeItem((AcDbSoftPointerId&)m_ObjId);

	return pFiler->filerStatus();
}

void CMAWinReact::modified(const AcDbObject* dbObj)
{
	assertNotifyEnabled();

	CMARectWindow *pRW1, *pRW2;
	AcDbEntity *pEnt;
	AcGePoint3d newLwrLeftPt;
	AcGeVector3d transVec;
	AcGeMatrix3d mat;
	Acad::ErrorStatus es;
	char strError[80];

    pRW1 = CMARectWindow::cast(dbObj);
    if(!pRW1)
	{
        const char *cstr = dbObj->isA()->name();
        acutPrintf("This is a %s.\n", cstr);
        acutPrintf("I only work with RectWindow custom entities. Sorry.\n");
        return;
    }
	
	newLwrLeftPt = pRW1->getWindowStartPoint();
	transVec = newLwrLeftPt - m_llPt;
	m_llPt = newLwrLeftPt;
	mat.setToTranslation(transVec);

	es = acdbOpenObject(pEnt, m_ObjId, AcDb::kForWrite);
    if(es != Acad::eOk)
	{
		if(es == Acad::eWasOpenForWrite) // 83
		{
			strcpy(strError, " (open for write)");
		}
		else if(es == Acad::eWasNotifying) // 85
		{
			strcpy(strError, " (open for notify)");
		}
		else if(es == Acad::eWasErased)
		{
			strcpy(strError, " (was erased)"); // 80
		}
		else
		{
			strcpy(strError, "");
		}
        acutPrintf("\nacdbOpenObject in WinReact failed with error %d%s.\n",
					es, strError);
        return;
    }

    pRW2 = CMARectWindow::cast(pEnt);
    if (!pRW2) {
        const char *pCstr = pEnt->isA()->name();
        acutPrintf("This reactor is attached to a %s.\n", pCstr);
        acutPrintf("It should only be attached to RectWindow custom Entities. Sorry.\n");
        pEnt->close();
        return;
    }

	pRW2->transformBy(mat);

	pRW2->close();

	AcDbObject::modified(dbObj);
}

